package com.ivanbiz.generic;

import java.util.Date;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 *
 * @author User
 */
public class GenericDAOImpl extends HibernateDaoSupport implements GenericDAO {

    public void delete(Object obj) throws Exception {
        try {
            getSession().delete(obj);
        } catch (Exception e) {
            throw e;
        }
    }

    public Object load(Class clasz, long id) throws Exception {
        try {
            return getSessionFactory().getCurrentSession().get(clasz, id);
        } catch (Exception e) {

            throw e;
        }

    }

    public void save(Object obj) throws Exception {
        try {
            getSession().save(obj);
        } catch (Exception e) {
            throw e;
        }
    }

    public List getAll(Class claz) throws Exception {
        try {
            Criteria criteria = getSession().createCriteria(claz);
            return criteria.list();
        } catch (Exception e) {
            throw e;
        }
    }

    public void update(Object obj) throws Exception {
        try {
            getSession().update(obj);
        } catch (Exception e) {
            throw e;
        }
    }

    public List getDataByLike(Class clasImpl, String variable, Object input) throws Exception {
        try {
            Criteria crit = getSessionFactory().getCurrentSession().createCriteria(clasImpl);
            crit.add(Restrictions.like(variable, "%" + input + "%"));
            List list = crit.list();
            return list;
        } catch (Exception e) {
            throw e;
        }
    }

    public List getDataByEquals(Class clazImpl, String variable, Object input) throws Exception {
        try {
            Criteria crit = getSessionFactory().getCurrentSession().createCriteria(clazImpl);
            crit.add(Restrictions.eq(variable, input));
            List list = crit.list();
            return list;
        } catch (Exception e) {
            throw e;
        }
    }

    public Object getDataByEqual(Class clazImpl, String variable, Object input) throws Exception {
        try {
            Criteria crit = getSessionFactory().getCurrentSession().createCriteria(clazImpl);
            crit.add(Restrictions.eq(variable, input));
            Object obj = crit.uniqueResult();
            return obj;
        } catch (Exception e) {
            throw e;
        }
    }

    public List getData(Class claz, Date start, Date end) throws Exception {
        try {
            start.setHours(0);
            start.setMinutes(0);
            start.setSeconds(0);
            end.setHours(0);
            end.setMinutes(0);
            end.setSeconds(0);
            Criteria crit = getSessionFactory().getCurrentSession().createCriteria(claz).add(Restrictions.ge("createDate", start)).add(Restrictions.le("createDate", end));
            List list = crit.list();
            return list;
        } catch (Exception e) {
            throw e;
        }
    }

    public Session getSessionByGeneric() throws Exception {
        return getSessionFactory().getCurrentSession();
    }
}
